package com.zzy.mccjt.retelimiter;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @program: mccjt-cloud
 * @description:限流SpringBoot自动配置
 * @author: zhouyang.zheng
 * @create: 2022-08-02 14:27
 **/
@Configuration
@ConditionalOnClass(RedisTemplate.class)
@EnableConfigurationProperties({RateLimiterProperties.class})
public class RateLimiterAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public RateLimiterInterceptor rateLimiterInterceptor(RateLimiterTemplate rateLimiterTemplate,
                                                         RateLimiterProperties rateLimiterProperties) {
        return new RateLimiterInterceptor(rateLimiterTemplate, rateLimiterProperties);
    }

    @Bean
    @ConditionalOnMissingBean
    public RateLimiterTemplate rateLimiterTemplate(RedisTemplate redisTemplate,
                                                   RateLimiterProperties rateLimiterProperties) {
        return new RateLimiterTemplate(redisTemplate, rateLimiterProperties);
    }
}
